package com.yuyou.fn.provider.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.google.common.collect.Collections2;
import com.yuyou.fn.api.BaseDataService;
import com.yuyou.fn.api.dto.*;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.entity.StudentSchool;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IBaseSubjectService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.educational.service.IStudentSchoolService;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.portal.entity.CourseDisplayConfig;
import com.yuyou.fn.portal.entity.CourseFilterCondConfig;
import com.yuyou.fn.portal.service.IConfigService;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ExposeBaseDataServiceImpl implements BaseDataService{

    @Resource
    private IBaseGradeService baseGradeService;

    @Resource
    private IBaseSubjectService baseSubjectService ;

    @Resource
    private IPeriodService periodService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IStudentSchoolService studentSchoolService;

    @Resource
    private IConfigService configService;

    @Override
    public Collection<GradeDto> findAllGrade() {
        return Collections2.transform(baseGradeService.findAllFromCache(), ( baseGrade) -> {
            GradeDto gradeDto = new GradeDto();
            gradeDto.setGradeId(baseGrade.getGradeId());
            gradeDto.setGradeName(baseGrade.getGradeName());
            return gradeDto;
        });
    }

    @Override
    public Collection<SubjectDto> findAllSubject() {
        return Collections2.transform(baseSubjectService.findAllFromCache(), ( baseSubject) ->{
            SubjectDto subjectDto = new SubjectDto() ;
            subjectDto.setSubjectId(baseSubject.getSubjectId());
            subjectDto.setSubjectName(baseSubject.getSubjectName());
            return subjectDto;
        });
    }

    @Override
    public Collection<AreaTeamDto> findAllAreaTeam() {
        List<AreaTeam> allAreaTeams = areaTeamService.findAllAreaTeam();
        return Collections2.transform(allAreaTeams,t ->{
            AreaTeamDto areaTeamDto = new AreaTeamDto();
            areaTeamDto.setAreaTeamId(t.getAreaTeamId());
            areaTeamDto.setAreaTeamName(t.getName());
            return areaTeamDto ;
        });
    }

    @Override
    public Collection<PeriodDto> findNearPeriod(Long  areaTeamId) {

        List<Period> periods = periodService.findNearPeriod(areaTeamId, 2);

        if(periods == null ){
            return Collections.EMPTY_LIST;
        }

        return Collections2.transform(periods,(p)->{
            PeriodDto periodDto = new PeriodDto();
            BeanUtils.copyProperties(periodDto,p);
            return periodDto;
        });
    }

    @Override
    public Collection<StudentSchoolDto> findStudentSchool(String province, String city, String district, String query) {
        List<StudentSchool> studentSchools = studentSchoolService.searchStudentSchool(province, city, district, query);

        if(studentSchools == null ){
            return Collections.EMPTY_LIST ;
        }
        return Collections2.transform(studentSchools,(input)->{
            StudentSchoolDto studentSchoolDto = new StudentSchoolDto() ;
            BeanUtils.copyProperties(studentSchoolDto,input);
            return studentSchoolDto;
        });
    }

    @Override
    public String findCourseDisplayConfigFromCache(Long areaTeamId) {
        CourseFilterCondConfig courseFilterCondConfig = configService.findCourseFilterCondConfigFromCache(areaTeamId);
        return JSONObject.toJSON(courseFilterCondConfig).toString();
    }
}
